package com.likou;

import java.util.*;

/**
 * Author: 陈桂明
 * Date: 2025/3/29 19:57
 * Description: q49
 */
public class q49 {
    public static void main(String[] args) {
        String[] eat = new String[]{"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> lists = new q49().groupAnagrams(eat);
        System.out.println(lists);
    }

    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (String s : strs) {
            // 对单个字符串进行排序
            char[] array = s.toCharArray();
            Arrays.sort(array);
            String key = new String(array);
            System.out.println(key);

            if (!map.containsKey(key)) {
                map.put(key, new ArrayList<>());
            }
            map.get(key).add(s);
        }

        return new ArrayList<List<String>>(map.values());
    }
    //
    // public List<List<String>> groupAnagrams(String[] strs) {
    //     HashMap<Long, List<String>> hash = new HashMap<>();
    //     //每个字母对应一个质数
    //     int[] prime = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101,
    //             103 };
    //     for (int i = 0; i < strs.length; i++) {
    //         Long key = 1L;
    //         //累乘得到 key
    //         for (int j = 0; j < strs[i].length(); j++) {
    //             key *= prime[strs[i].charAt(j) - 'a'];
    //         }
    //         if (hash.containsKey(key)) {
    //             hash.get(key).add(strs[i]);
    //         } else {
    //             List<String> temp = new ArrayList<String>();
    //             temp.add(strs[i]);
    //             hash.put(key, temp);
    //         }
    //
    //     }
    //     return new ArrayList<List<String>>(hash.values());
    // }
}

